package cn.tarena.ht.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import cn.tarena.ht.tool.Md5HashTool;
/**自定义Shiro加密方法
 */
public class AuthCredential extends SimpleCredentialsMatcher{
	
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		//强转登陆令牌的数据类型
		UsernamePasswordToken loginToken = (UsernamePasswordToken) token;
		//获取用户名
		String username = loginToken.getUsername();
		//获取用户密码
		String password = String.valueOf(loginToken.getPassword());
		//进行加密
		String md5Password = Md5HashTool.getPassword(username, password);
		//将加密后的密码存到登陆令牌中
		loginToken.setPassword(md5Password.toCharArray());
		//将用户输入的内容与真实数据进行匹配，直接返回匹配结果
		return super.doCredentialsMatch(loginToken, info);
	}
}
